<?php
/**
 * 面向过程，就是按一步一步去执行
 */
$server = new swoole_websocket_server("0.0.0.0", 8812);

//设置静态页
$server->set([
    'enable_static_handler' => true,
    'document_root' => '/home/vagrant/Code/demo/data'
]);

//监听websocket打开事件
$server->on('open','onOpen');
function onOpen($server,$request){
   print_r($request->fd);
}

//监听ws消息事件
$server->on('message', function (swoole_websocket_server $server, $frame) {
    echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
    $server->push($frame->fd, "this is server");
});

$server->on('close', function ($ser, $fd) {
    echo "client {$fd} closed\n";
});

$server->start();